<!DOCTYPE html>
<html lang="en">

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="format-detection" content="telephone=no">
		<title>51微贷</title>
		<link rel="stylesheet" type="text/css" href="css/reset.css">
		<link rel="stylesheet" type="text/css" href="css/common.css">
		<script type="text/javascript" src="js/rem.js"></script>
	</head>

	<body>
		<div class="nav-bar">
			<ul class="clearfix">
				<li class="active" data-href="html/home.html">
					<i class="icon-1"></i>
					<span>首页</span>
				</li>
				<li data-href="html/credit.html">
					<i class="icon-2"></i>
					<span>理财</span>
				</li>
				<li data-href="html/discovery/discovery.html">
					<i class="icon-3"></i>
					<span>发现</span>
				</li>
				<li data-href="html/account.html">
					<i class="icon-4"></i>
					<span>我</span>
				</li>
			</ul>
		</div>
		<script type="text/javascript" src="js/md5.min.js"></script>
		<script type="text/javascript" src="js/zepto.min.js"></script>
		<script type="text/javascript" src="js/mui.min.js"></script>
		<script type="text/javascript" src="js/common.js"></script>

		<script type="text/javascript">
			var subpages = [
				'html/home.html',
				'html/credit.html',
				'html/discovery/discovery.html',
				'html/account.html'
			];
			var subpage_style = {
				top: '0px',
				bottom: '49px',
				bounce: 'vertical'
			};
			document.addEventListener("plusready", function() {
//				plus.storage.setItem('user_id','19');
				plus.runtime.setBadgeNumber(0);
				plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) {
					var key = wgtinfo.version.replace('.', '').replace(' ', '') + 'lauchFlag';
					console.log('lauchFlag:' + plus.storage.getItem(key));
					//读取本地存储，检查当前版本是否为首次启动
					if(plus.storage.getItem(key)) {
						//有值，说明已经显示过了，无需显示；关闭splash页面；
						plus.navigator.closeSplashscreen();
						plus.navigator.setFullscreen(false);
					} else {
						//显示启动导航
						mui.openWindow({
							url: 'html/guide.html',
							styles: {
								popGesture: "none"
							},
							show: {
								aniShow: 'none'
							},
							waiting: {
								autoShow: false
							}
						});
					}
				});
				//初始化用户登录页面
				judgelogin();
			}, false);

			mui.init({
				subpages: [{
					url: subpages[3],
					id: subpages[3],
					styles: subpage_style,
				}, {
					url: subpages[2],
					id: subpages[2],
					styles: subpage_style,
				}, {
					url: subpages[1],
					id: subpages[1],
					styles: subpage_style,
				}, {
					url: subpages[0],
					id: subpages[0],
					styles: subpage_style,
				}],
				swipeBack: false,
				//statusBarBackground: '#20adff', 
				keyEventBind: {
					backbutton: true,
					menubutton: true
				},
				beforeback: function() {
					//窗口关闭前处理其他业务详情点击 ↑ "关闭页面"链接查看
				}
			});

			var aniShow = {};
			//当前激活选项
			var activeTab = subpages[0];
			//选项卡点击事件
			mui('.nav-bar ul').on('tap', 'li', function(e) {
				$(this).addClass('active').siblings('li').removeClass('active');
				var targetTab = this.getAttribute('data-href');
				//显示目标选项卡
				//若为iOS平台或非首次显示，则直接显示
				if(mui.os.ios || aniShow[targetTab]) {
					plus.webview.show(targetTab);
				} else {
					//否则，使用fade-in动画，且保存变量
					//					var temp = {};
					//					temp[targetTab] = "true";
					//					mui.extend(aniShow, temp);					
					//					plus.webview.show(targetTab, "fade-in", 300);
					plus.webview.show(targetTab);
				}
				//更改当前活跃的选项卡
				activeTab = targetTab;
				subpages.forEach(function(e) {
					if(targetTab != e) {
						plus.webview.hide(e); //隐藏
					}
				});
			});

			//			//初始化所有bar页面
			//			function inittabitemWebviews() {
			//				for(var i = 0; i < subpages.length; i++) {
			//					barItemWebviewArray[i] = mui.preload({
			//						id: subpages[i],
			//						url: subpages[i],
			//						styles: subpage_style,
			//						waiting: {
			//							autoShow: false
			//						}
			//					});
			//					barItemWebviewArray[i].hide();
			//					mainWebview.append(subpages[i]);
			//				}
			//				barItemWebviewArray[0].show();
			//			}
			//预加载登录页面
			function judgelogin() {
				//判断是否已经登录成功
				if(!plus.storage.getItem('user_id')) {
					mui.preload({
						url: 'html/passport.html',
						id: 'html/passport.html'
					});
				}

				//				getAddressBook();
			}

//			//获取用户所有的通讯录
//			function getAddressBook() {
//				plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, function(addressbook) {
//					addressbook.find(["displayName", "phoneNumbers"], function(contacts) {
//						console.log(JSON.stringify(contacts));
//					}, function() {
//						console.log("error");
//					}, {
//						filter: {},
//						multiple: true
//					});
//				}, function(e) {
//					console.log("获取通讯录对象出错: " + e.message);
//				});
//			}

			/*
			 * 推送数据绑定
			 * */
			document.addEventListener("plusready", function() {
				//延迟两秒处理
				setTimeout(function() {
					return devicePushBind();
				}, 2000);
			}, false);

			function devicePushBind() {
				if(plus.storage.getItem('push-status') == 'true') {
					console.log('当前设备已经绑定推送数据数据');
					return false;
				}
				if(plus.storage.getItem('user_id')) {
					return bindPushUser();
				} else {
					return plusreadyPush();
				}
			}

			function plusreadyPush() {
				var info = plus.push.getClientInfo();
				var os_type = mui.os.ios ? 2 : 1;
				plus.storage.setItem("os-type", os_type + "");
				var data = {
					client_id: info.clientid,
					token: info.token,
					os_type: os_type
				};
				
				console.log('推送设备数据:' + JSON.stringify(data));
				mui.ajax(base + 'infranstructure/pushdevice', {
					data: data,
					dataType: 'json',
					type: 'post',
					headers: sign(md5),
					success: function(data) {
						console.info('推送设备记录:' + JSON.stringify(data));
					},
					error: function(data) {
						console.error('推送设备记录出错:' + JSON.stringify(data));
					}
				});
			}

			function bindPushUser() {
				var info = plus.push.getClientInfo();
				console.log('获取设备数据：' + JSON.stringify(info));
				var data = {
					user_id: plus.storage.getItem('user_id'),
					client_id: info.clientid,
					token: info.token,
					os_type: mui.os.ios ? 2 : 1
				};
				console.log('绑定用户推送设备发送数据:' + JSON.stringify(data));
				mui.ajax(base + 'infranstructure/pushuserbind', {
					data: data,
					dataType: 'json',
					type: 'post',
					headers: sign(md5),
					success: function(data) {
						plus.storage.setItem('push-status', data.result + ''); //保存推送状态
						console.info('绑定用户推送设备返回数据:' + JSON.stringify(data));
					},
					error: function(data) {
						console.error('绑定用户推送设备失败 原因:' + data);
					}
				});
			}
		</script>
	</body>

</html>